package com.wtx.atguigu.nio;

import java.nio.IntBuffer;

/**
 * @description: NIO buffer的说明
 * @author: wtx
 * @createDate: 2020/6/8
 */
public class BasicBuffer {
    public static void main(String[] args) {


        // 创建大小为5的buffer
        // 底层存放数据的数组 final int[] hb;
        // mark = -1
        // position = 0
        // limit = 5
        // capacity = 5
        IntBuffer buffer = IntBuffer.allocate(5);

        //写
        buffer.put(10);
        buffer.put(20);
        buffer.put(30);


        //读写切换
        /*
        *public final Buffer flip() {
            limit = position;
            position = 0;
            mark = -1;
            return this;
           }*/
        // position = 0
        // limit = 3
        buffer.flip();

        // 从20开始读
        buffer.position(1);
        // 读到20
        buffer.limit(2);

        //读
        while (buffer.hasRemaining())
            // position会自动加1
            System.out.println(buffer.get());


    }
}
